package com.gs.m_my_goods.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.gs.common.API
import com.gs.common.data.Good
import com.gs.common.data.Order
import com.gs.m_my_goods.R


class MyGoodsAdapter(context: Context, data: ArrayList<Good>): RecyclerView.Adapter<MyGoodsAdapter.MyGoodsViewHolder>() {

    private val data: ArrayList<Good>
    private val context: Context

    private var mOnItemUpdateBtnClickListener: OnMyGoodsItemUpdateBtnClickListener? = null
    private var mOnItemDeleteBtnClickListener:  OnMyGoodsItemDeleteBtnClickListener? = null


    inner class MyGoodsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val goodTitle: TextView
        val goodImg: ImageView
        val goodDeatilTitle: TextView
        val goodCount: TextView
        val goodPrice: TextView
        val updateBtn: TextView
        val deleteBtn: TextView

        init {
            goodTitle = itemView.findViewById(R.id.good_title_tv)
            goodDeatilTitle = itemView.findViewById(R.id.good_detail_title_tv)
            goodImg = itemView.findViewById(R.id.good_img)
            goodCount = itemView.findViewById(R.id.good_count_tv)
            goodPrice = itemView.findViewById(R.id.good_price_tv)
            updateBtn = itemView.findViewById(R.id.update_good_btn)
            deleteBtn = itemView.findViewById(R.id.delete_good_btn)

            updateBtn.setOnClickListener {
                if (data.size == 0) return@setOnClickListener
                mOnItemUpdateBtnClickListener?.onClick(adapterPosition, data[adapterPosition].id)
            }

            deleteBtn.setOnClickListener {
                if (data.size == 0) return@setOnClickListener
                mOnItemDeleteBtnClickListener?.onClick(adapterPosition, data[adapterPosition].id)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyGoodsViewHolder {
        val view = View.inflate(context, R.layout.my_goods_item_layout, null)
        return MyGoodsViewHolder(view)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MyGoodsViewHolder, position: Int) {
        holder.goodTitle.text = "* ${data[position].title} *"
        holder.goodDeatilTitle.text = data[position].detail_title
        Glide.with(context).load("http://${API.IP}:${API.PORT}${data[position].picture_path}").into(holder.goodImg)
        holder.goodCount.text = data[position].count.toString()
        holder.goodPrice.text = data[position].price.toString()
    }

    interface OnMyGoodsItemUpdateBtnClickListener {
        fun onClick(position: Int, goodId: Int)
    }

    interface OnMyGoodsItemDeleteBtnClickListener {
        fun onClick(position: Int, goodId: Int)
    }

    fun setMyGoodsItemUpdateBtnClickListener(listener: OnMyGoodsItemUpdateBtnClickListener) {
        mOnItemUpdateBtnClickListener = listener
    }

    fun setMyGoodsItemDeleteBtnClickListener(listener: OnMyGoodsItemDeleteBtnClickListener) {
        mOnItemDeleteBtnClickListener = listener
    }

    init {
        this.data = data
        this.context = context
    }

}